package com.xintonghua.bussiness.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xintonghua.base.adapter.BaseAdapter;
import com.xintonghua.base.widget.ShowAllListView;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.bean.GoodsListBean;
import com.xintonghua.bussiness.bean.ServiceItems;
import com.xintonghua.bussiness.bean.StockOrderBean;
import com.xintonghua.bussiness.event.StockEventBus;
import com.xintonghua.bussiness.ui.user.manage.SelectSaltActivity;
import com.xintonghua.bussiness.util.GlideUtils;
import com.xintonghua.bussiness.util.MyUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：wanxianyi
 * 时间：2018/1/11.
 * 包名：com.xintonghua.bussiness.adapter
 */

public class StockManageAdapter extends BaseAdapter<StockOrderBean, StockManageAdapter.StockManageViewHolder> {


    public StockManageAdapter(Activity mActivity, List<StockOrderBean> list) {
        super(mActivity, list);
    }

    @Override
    public StockManageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.adapter_stock_manage, parent, false);
        return new StockManageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(StockManageViewHolder holder, int position) {
        final StockOrderBean bean = list.get(position);
        holder.tvOrderNo.setText("订单号：" + bean.getNumber());
        holder.tvOrderTime.setText(bean.getCreate_time());
        if (bean.getStatus() == 1) {
            holder.tvSureOrder.setText("确认收货");
        } else if (bean.getStatus() == 2) {
            holder.tvSureOrder.setText("已签收");
        }
        holder.tvSureOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(bean);
            }
        });
        holder.tvSelectSalt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new StockEventBus(bean));
            }
        });
        addItem(holder.lvGoods, bean.getGoodsList());

    }

    private void addItem(LinearLayout ll, List<GoodsListBean> items) {
        ll.removeAllViews();
        for (GoodsListBean bean : items) {
            View view = inflater.inflate(R.layout.item_goods, ll, false);
            ImageView iv_order_icon = view.findViewById(R.id.iv_order_icon);
            TextView tv_goods_name = view.findViewById(R.id.tv_goods_name);
            TextView tv_goods_count = view.findViewById(R.id.tv_goods_count);
            TextView tv_goods_price = view.findViewById(R.id.tv_goods_price);
            GlideUtils.load(mActivity, iv_order_icon, bean.getGoods_img());
            tv_goods_name.setText(bean.getGoods_name());
            tv_goods_price.setText(MyUtils.getMoney(mActivity, bean.getGoods_money()));
            tv_goods_count.setText("x" + bean.getGoods_count());
            ll.addView(view);
        }

    }


    class StockManageViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_order_no)
        TextView tvOrderNo;
        @BindView(R.id.tv_order_time)
        TextView tvOrderTime;
        @BindView(R.id.lv_goods)
        LinearLayout lvGoods;
        @BindView(R.id.tv_select_salt)
        TextView tvSelectSalt;
        @BindView(R.id.tv_sure_order)
        TextView tvSureOrder;

        public StockManageViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
